<?php

class FinancedetailModel extends DBModel
{
	protected $_table = '{{financedetail}}';
	protected $_id = 'fid';
	
	public function getListByUserId($userid)
	{
		$where['condition'] = 'uid = :uid';
		$where['select'] = 'howmuch,createtime';
		$params[':uid'] = $userid;
		$where['order'] = 'createtime DESC';
		return $this->findAll($where, $params);
	}
	
	public function isExist($uid, $ordernum)
	{
		$where['select'] = 'fid';
		$where['condition'] = 'uid = :uid and ordernum = :ordernum';
		$params[':uid'] = $uid;
		$params[':ordernum'] = $ordernum;
		return $this->findScalar($where, $params);
	}
	
	public function firstAdd($userid, $howmuch)
	{
		$where['select'] = 'count(*)';
		$where['condition'] = 'uid = :uid and paytype = :paytype';
		$params = array(':uid' => $userid, ':paytype' => '赠送第一次充值');
		if ($this->findScalar($where, $params) == 0) {
			$data = array(
				'uid' => $userid,
				'paytype' => '赠送第一次充值',
				'howmuch' => $howmuch,
				'ordernum' => time(),
				'createtime' => time()
			);
			if ($this->model('Financedetail')->insert($data) !== false) {
				$this->model('UserMoney')->addMoney($userid, $howmuch);
			}
		}
	}
}